<?php


class Model_Token extends PhalApi_Model_NotORM {

    function getValidByToken($token) {
        $where = array(
            'token'=>$token,
            'data_flag'=>1,
        );
        return DI()->notorm->token->where($where)->fetch();
    }

    function createToken($userId){
        DI()->notorm->token->where('user_id',$userId)->update(array('data_flag'=>0));
        $token = getUniName();
        $data = array(
            'user_id'=>$userId,
            'token'=>$token,
            'record_stamp'=> time(),
            'ip'=>getIP(),
        );
        $rs = DI()->notorm->token->insert($data);
        return ($rs)? $token : '';
    }

    function invalidToken($token){
        return DI()->notorm->token->where('token',$token)->update(array('data_flag'=>0));
    }
}
